//
//  SHD_ExcellectViewController.m
//  B段项目
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_ExcellectViewController.h"

@interface SHD_ExcellectViewController ()
@property(nonatomic, strong)XMN_ProgressHUD *HUD;
@end

@implementation SHD_ExcellectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"良品";
    
    _HUD = [[XMN_ProgressHUD alloc]initWithFrame:self.view.frame];
    _HUD.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.9 alpha:1];
    _HUD.XMN_label.text = @"Waiting...";
    [self.view addSubview:_HUD];
}
-(void)loadView
{
    [super loadView];
    _dataArray = [NSMutableArray array];
    _exceTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, (667 - 64) * FitHeight)];
    _exceTableView.delegate = self;
    _exceTableView.dataSource = self;
    [_exceTableView NightWithType:UIViewColorType2];
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    _exceTableView.header = header;
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 1; i <= 9; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%lda", i]];
        [array addObject:image];
    }
    [header setImages:array duration:1 forState:MJRefreshStatePulling];
    header.lastUpdatedTimeLabel.hidden = YES;
    [header setTitle:@"加载中 ..." forState:MJRefreshStateRefreshing];
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    _exceTableView.footer = footer;
    [footer setImages:array duration:1 forState:MJRefreshStateRefreshing];
    [_exceTableView.header beginRefreshing];
    [self.view addSubview:_exceTableView];
    
   
}
-(void)getData
{
    if ([_exceTableView.header isRefreshing]) {
        _flag = 0;
    }
    else
    {
        _flag += 10;
    }
    [HTTPMethod getDataByString:@"http://api2.pianke.me/pub/shop" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&deviceid=5AA12B08-7AD2-4ED0-B4EB-571025443952&limit=10&start=%ld&version=3.0.6", _flag]] WithDataBlock:^(id data) {
        if (_flag == 0 && _dataArray != nil) {
            [_dataArray removeAllObjects];
        }
        NSArray *array = [[data objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            SHD_ExectMedol *exect = [[SHD_ExectMedol alloc]init];
            [exect setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:exect];
        }
        [_exceTableView reloadData];
        [_exceTableView.footer endRefreshing];
        [_exceTableView.header endRefreshing];
        [_HUD removeFromSuperview];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    SHD_ExectTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[SHD_ExectTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count == 0) {
        return cell;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.exectModel = _dataArray[indexPath.row];
    return cell;
}
#pragma mark - 跳转web页
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHD_ExectWebViewController *sec = [[SHD_ExectWebViewController alloc]init];
    sec.Model = _dataArray[indexPath.row] ;
    [self.navigationController pushViewController:sec animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 260 * FitHeight;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
